package com.zqweb.gulimall.coupon.service.impl;

import com.zqweb.gulimall.coupon.entity.SeckillSkuRelationEntity;
import com.zqweb.gulimall.coupon.service.SeckillSkuRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zqweb.common.utils.PageUtils;
import com.zqweb.common.utils.Query;

import com.zqweb.gulimall.coupon.dao.SeckillSessionDao;
import com.zqweb.gulimall.coupon.entity.SeckillSessionEntity;
import com.zqweb.gulimall.coupon.service.SeckillSessionService;
import org.springframework.util.StringUtils;


@Service("seckillSessionService")
public class SeckillSessionServiceImpl extends ServiceImpl<SeckillSessionDao, SeckillSessionEntity> implements SeckillSessionService {
    @Autowired
    private SeckillSkuRelationService seckillSkuRelationService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<SeckillSessionEntity> queryWrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        if(!StringUtils.isEmpty(key)){
            queryWrapper.eq("id",key).or().like("name",key).or().eq("status",key);
        }
        IPage<SeckillSessionEntity> page = this.page(
                new Query<SeckillSessionEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    // 获取最近3天要参与秒杀的session活动及所关联的所有秒杀商品
    public List<SeckillSessionEntity> getLate3DaySession() {
        List<SeckillSessionEntity> seckillSessionEntities = this.baseMapper.selectList(
                new QueryWrapper<SeckillSessionEntity>().between("start_time",getStartDateTime(),getEndDateTime())
        );
        if(seckillSessionEntities != null && seckillSessionEntities.size() > 0){
            seckillSessionEntities = seckillSessionEntities.stream().map(seckillSessionEntity -> {
                Long sessionId = seckillSessionEntity.getId();// 秒杀活动场次id
                List<SeckillSkuRelationEntity> relationEntities = seckillSkuRelationService.list(new QueryWrapper<SeckillSkuRelationEntity>().eq("promotion_session_id", sessionId));
                seckillSessionEntity.setRelations(relationEntities);
                return seckillSessionEntity;
            }).collect(Collectors.toList());
        }
        return seckillSessionEntities;
    }
    // 获取到当前时间对应的startDateTime
    private String getStartDateTime(){
        LocalDate nowDate = LocalDate.now();
        LocalTime minTime = LocalTime.MIN;
        LocalDateTime startDateTime = LocalDateTime.of(nowDate, minTime);
        String startDateTimeFormat = startDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        System.out.println("nowDate = " + nowDate + ",minTime = " + minTime + ",startDateTime = " + startDateTimeFormat);
        return startDateTimeFormat;
    }
    // 获取到当前时间对应的endTime（3天范围）
    private String getEndDateTime(){
        LocalDate nowDate = LocalDate.now();
        LocalDate thirdDayLocalDate = nowDate.plusDays(2L);
        LocalTime maxTime = LocalTime.MAX;
        LocalDateTime endDateTime = LocalDateTime.of(thirdDayLocalDate, maxTime);
        String endDateTimeFormat = endDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        System.out.println("thirdDayLocalDate = " + thirdDayLocalDate + ",maxTime = " + maxTime + ",endDateTime = " + endDateTimeFormat);
        return endDateTimeFormat;
    }

}